package com.service;

import com.bean.DateInfo;
import com.bean.ShoppingInfo;
import com.bean.other.MetaInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.DateInfoMapper;
import com.mapper.ShoppingInfoMapper;
import com.mapper.repository.DateInfoRepository;
import com.mapper.repository.ShoppingInfoRepository;
import com.service.impl.DateInfoService;
import com.service.impl.ShoppingInfoService;
import com.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class ShoppingInfoServiceImpl implements ShoppingInfoService {

    @Autowired
    private ShoppingInfoRepository shoppingInfoRepository;

    @Autowired
    private ShoppingInfoMapper shoppingInfoMapper;

    @Override
    public String insert(ShoppingInfo shoppingInfo) {
        shoppingInfo.setId(UUID.randomUUID().toString());
        shoppingInfo.setCreatetime(LocalDateTime.now());
        shoppingInfoRepository.save(shoppingInfo);
        return "新增成功";
    }

    @Override
    public String update(ShoppingInfo shoppingInfo) {
        shoppingInfo.setUpdatetime(LocalDateTime.now());
        shoppingInfoRepository.save(shoppingInfo);
        return "更新成功";
    }

    @Override
    public String delete(ShoppingInfo shoppingInfo) {
        shoppingInfoRepository.delete(shoppingInfo);
        return "删除成功";
    }

    @Override
    public PageInfo<ShoppingInfo> select(ShoppingInfo shoppingInfo, MetaInfo metaInfo) {
        //PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        PageHelper.startPage(metaInfo.getPage(),metaInfo.getSize());
        List<ShoppingInfo> list=shoppingInfoMapper.select(shoppingInfo);

        PageInfo<ShoppingInfo> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public ShoppingInfo get(String id) {
        Optional<ShoppingInfo> optional=shoppingInfoRepository.findById(id);
        if(optional.isPresent()){
            return optional.get();
        }
        return null;
    }


}
